<?php
/**
 * @see ZendX_JQuery_View_Helper_UiWidget
 */
require_once "ZendX/JQuery/View/Helper/UiWidget.php";

/**
 * Помощник вида, реализующий списки с использованием плагина Chosen
 * 
 * @category Phorm
 * @package JQuery_View_Helper
 * @name /Phorm/JQuery/View/Helper/Chosen.php
 * @copyright phorm.ru
 * @since 06.11.2012
 * @author vladimir@kachalov.net
 */

class Phorm_JQuery_View_Helper_Chosen extends ZendX_JQuery_View_Helper_UiWidget {
	
	public function Chosen($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n") {
		
		$params = $this->_prepareAttributes($name, $value, $attribs);
		
		$translate = $this->view->translate()->getTranslator();
		
		/**
		 * В режиме совместимости с ИЕ7 заменяем на SelectBox
		 */
		
		if(isset($attribs['compatibility']) && $attribs['compatibility'] == true && stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) {
			
			return $this->view->selectBox($name,$value,$attribs,$options,$listsep);
			
		}
		
		$this->jquery->addJavascriptFile($this->view->BaseUrl().'/ui/chosen/chosen.jquery.min.js');
		$this->jquery->addStyleSheet($this->view->BaseUrl().'/ui/chosen/chosen.css');
		
		$js = sprintf('%s("#%s").chosen({ no_results_text: "' . $translate->_('Нет вариантов для') . '", default_text: "' . $translate->_('Выберите') . '", placeholder_text_multiple: "' . $translate->_('Можно выбрать несколько') . '" });',
            ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
            $params['id']
        );
        $this->jquery->addOnLoad($js);
		
		return $this->view->formSelect($name, $value, $attribs, $options, $listsep);
		
	}
	
}